/**
 * Copyright 2015-2016 eyaomall.cn
 * All rights reserved.
 *
 * @project
 * @author jackey
 * @version 1.0
 * @date 2016-01-06
 */
package com.eyaomall.wxmall.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eyaomall.wxmall.common.error.BECode;
import com.eyaomall.wxmall.common.helper.MemberHelper;
import com.eyaomall.wxmall.model.po.ShoppingChart;
import com.eyaomall.wxmall.service.ShopService;
import com.eyaomall.wxmall.service.ShoppingChartService;
import com.github.pagehelper.StringUtil;
import com.lycheeframework.core.cmp.api.http.GsnData;
import com.lycheeframework.core.cmp.api.http.GsnResponse;
import com.lycheeframework.core.cmp.kit.PageHTTPObject;
import com.lycheeframework.core.exception.ErrorCodeException;
/**
 * 我的购物车
 * @author pro
 *
 */
@Controller
@RequestMapping("shopping_chart")
public class ShoppingChartController {
	
	@Autowired
	private ShoppingChartService shoppingChartService;
	
	@Autowired
	private ShopService shopService;

	/**
	 * 购物车首页
	 * @return
	 */
	@RequestMapping("index")
	public String index(Map<String,Object> data){
		data.put("shop", shopService.queryById(MemberHelper.getShopId()));
		return "shopping_chart/index";
	}
	
	/**
	 * 分页获取购物车的数据
	 * @param httpObject
	 * @return
	 */
	@RequestMapping("page_data")
	public @ResponseBody GsnData page(PageHTTPObject httpObject){
		
		return GsnResponse.toData(shoppingChartService.page(MemberHelper.getId(), httpObject));
	}
	
	/**
	 * 查询购物车内的产品数量
	 * @return
	 */
	@RequestMapping("count")
	public @ResponseBody GsnData queryCount(){
		
		return GsnResponse.toData(shoppingChartService.count(MemberHelper.getId()));
	}
	
	/**
	 * 创建或者新增
	 * @param chart
	 * @return
	 */
	@RequestMapping("create_or_update")
	public @ResponseBody GsnData createOrUpdate(ShoppingChart chart){
		if(chart.getGoodsId() == null || chart.getGoodsId() <= 0)
			throw new ErrorCodeException(BECode.shopping_chart_7000000);
		
		if(chart.getNum() == null || chart.getNum() <= 0)
			throw new ErrorCodeException(BECode.shopping_chart_7000001);
		
		return GsnResponse.toData(shoppingChartService.addOrUpdate(chart.getGoodsId(), chart.getNum()));
	}
	
	/**
	 * 创建或者新增
	 * @param chart
	 * @return
	 */
	@RequestMapping("join")
	public @ResponseBody GsnData join(ShoppingChart chart){
		if(chart.getGoodsId() == null || chart.getGoodsId() <= 0)
			throw new ErrorCodeException(BECode.shopping_chart_7000000);
		
		if(chart.getNum() == null || chart.getNum() <= 0)
			throw new ErrorCodeException(BECode.shopping_chart_7000001);
		chart.setMemberId(MemberHelper.getId());
		
		ShoppingChart cc = shoppingChartService.query(chart.getGoodsId(), MemberHelper.getId());
		if(cc != null){
			chart.setNum(cc.getNum() + chart.getNum());
		}
		
		return GsnResponse.toData(shoppingChartService.addOrUpdate(chart.getGoodsId(), chart.getNum()));
	}
	
	/**
	 * 根据ID删除购物车商品信息
	 * @param id
	 * @return
	 */
	@RequestMapping("delete_by_id")
	public @ResponseBody GsnData deleteById(String ids){
		if(StringUtil.isEmpty(ids))
			throw new ErrorCodeException(BECode.shopping_chart_7000000);
		
		return GsnResponse.toData(shoppingChartService.deleteById(ids));
	}
}
